//添加论坛问题关注记录并更新关注数
exports.au_forum_content_concern = function (req, res) {

    var bodyParams = req.routeInfo.bodyParams;
    //最终结果
    var resultData = {};

    var contentConcernData = 0;

    var is_concern = 0;

    var concern = 0;

    var concern_category = 0;
    //任务流创建
    async.waterfall([
            //1.查询用户是否已关注当前问题
            function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchQuestionConcern + "/fqc_uid/" + bodyParams.fqc_uid + "/fqc_fcid/" + bodyParams.fqc_fcid,
                        method: 'POST',
                        data: {
                            fields: ["fqc_id", "is_delete"].join(",")
                        }
                    },
                    function (err, data) {
                        //console.log(data);
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }
                        is_concern = data.data;
                        cb();
                    }
                );

            },
            function (cb) {
                //已关注
                if (is_concern.length !== 0) {
                    if (is_concern[0].is_delete <= 0) {
                        R.SEND_HTTP(
                            req,
                            {
                                url: CORE_APIConfig.coreAPI.updateQuestionConcern,
                                method: 'POST',
                                data: {
                                    fqc_id: is_concern[0].fqc_id,
                                    is_delete: 1
                                }
                            },
                            function (err, data) {
                                // //console.log(data);
                                //判断是否有错误信息
                                if (err) {
                                    return RES.response(res, false, data);
                                }
                                resultData.cancleZan = "已取消关注";
                                cb();
                            }
                        );
                    } else {
                        R.SEND_HTTP(
                            req,
                            {
                                url: CORE_APIConfig.coreAPI.updateQuestionConcern,
                                method: 'POST',
                                data: {
                                    fqc_id: is_concern[0].fqc_id,
                                    is_delete: 0
                                }
                            },
                            function (err, data) {
                                // //console.log(data);
                                //判断是否有错误信息
                                if (err) {
                                    return RES.response(res, false, data);
                                }
                                resultData.reTouchZan = "关注成功";
                                cb();
                            }
                        );
                    }
                    //未关注
                } else {
                    R.SEND_HTTP(
                        req,
                        {
                            url: CORE_APIConfig.coreAPI.addQuestionConcern,
                            method: 'POST',
                            data: {
                                fqc_uid: bodyParams.fqc_uid,
                                fqc_fcid: bodyParams.fqc_fcid
                            }
                        },
                        function (err, data) {
                            // //console.log(data);
                            //判断是否有错误信息
                            if (err) {
                                return RES.response(res, false, data);
                            }
                            resultData.addData = "关注成功";
                            cb();
                        }
                    );
                }
            },
            //查询关注数
            function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchForumContent + "/fc_id/" + bodyParams.fqc_fcid,
                        method: 'POST',
                        data: {
                            fields: ["fc_id", "fc_fcid", "fc_collect_count"].join(",")
                        }
                    },
                    function (err, data) {
                        //console.log(data);
                        //判断是否有错误信息
                        if (err) {
                            return RES.response(res, false, data);
                        }

                        contentConcernData = data.data;
                        cb();
                    }
                );

            },
            //查询分类下的关注总数
            function (cb) {
                if (contentConcernData.length > 0) {
                    R.SEND_HTTP(
                        req,
                        {
                            url: CORE_APIConfig.coreAPI.searchForumContent + "/fc_fcid/" + contentConcernData[0].fc_fcid,
                            method: 'POST',
                            data: {
                                fields: ["fc_collect_count"].join(",")
                            }
                        },
                        function (err, data) {
                            //判断是否有错误信息
                            if (err) {
                                return RES.response(res, false, data);
                            }

                            var allConcernData = data.data;

                            for (var i = 0; i < allConcernData.length; i++) {

                                concern += parseInt(allConcernData[i].fc_collect_count);

                            }

                            cb();
                        }
                    );
                } else {
                    cb();
                }

            },

            //3.论坛问题关注数更新
            function (cb) {
                if (is_concern.length !== 0) {
                    if (is_concern[0].is_delete <= 0) {
                        R.SEND_HTTP(
                            req,
                            {
                                url: CORE_APIConfig.coreAPI.updateContent,
                                method: 'POST',
                                data: {
                                    fc_id: bodyParams.fqc_fcid,
                                    fc_collect_count: parseInt(contentConcernData[0].fc_collect_count) - 1
                                }
                            },
                            function (err, data) {
                                //判断是否有错误信息
                                if (err) {
                                    return RES.response(res, false, data);
                                }

                                resultData.updateData = data;
                                cb();
                            }
                        );
                    } else {
                        R.SEND_HTTP(
                            req,
                            {
                                url: CORE_APIConfig.coreAPI.updateContent,
                                method: 'POST',
                                data: {
                                    fc_id: bodyParams.fqc_fcid,
                                    fc_collect_count: parseInt(contentConcernData[0].fc_collect_count) + 1
                                }
                            },
                            function (err, data) {
                                //判断是否有错误信息
                                if (err) {
                                    return RES.response(res, false, data);
                                }

                                resultData.updateData = data;
                                cb();
                            }
                        );
                    }
                } else {
                    R.SEND_HTTP(
                        req,
                        {
                            url: CORE_APIConfig.coreAPI.updateContent,
                            method: 'POST',
                            data: {
                                fc_id: bodyParams.fqc_fcid,
                                fc_collect_count: parseInt(contentConcernData[0].fc_collect_count) + 1
                            }
                        },
                        function (err, data) {
                            //判断是否有错误信息
                            if (err) {
                                return RES.response(res, false, data);
                            }

                            resultData.updateData = data;
                            cb();
                        }
                    );
                }


            },

            //查询分类下的关注总数
            function (cb) {
                if (contentConcernData.length > 0) {
                    R.SEND_HTTP(
                        req,
                        {
                            url: CORE_APIConfig.coreAPI.searchForumContent + "/fc_fcid/" + contentConcernData[0].fc_fcid,
                            method: 'POST',
                            data: {
                                fields: ["fc_collect_count"].join(",")
                            }
                        },
                        function (err, data) {
                            //判断是否有错误信息
                            if (err) {
                                return RES.response(res, false, data);
                            }

                            var allConcernData = data.data;

                            for (var i = 0; i < allConcernData.length; i++) {

                                concern_category += parseInt(allConcernData[i].fc_collect_count);

                            }

                            cb();
                        }
                    );
                } else {
                    cb();
                }

            },
            //分类关注数更新
            function (cb) {

                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.forumUpdateCategory,
                        method: 'POST',
                        data: {
                            fc_id: contentConcernData[0].fc_fcid,
                            fc_concern_count: concern_category
                        }
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return RES.response(res, false, data);
                        }

                        resultData.updateData = data;
                        cb();
                    }
                );
            },
        ], function (err, result) {

            //返回最终拼接结果
            RES.response(res, true, resultData);

        }
    );

}